package com.bsj.power.pls.service.impl;

import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.constant.NumericalConstants;
import com.bsj.power.common.def.dto.bindCardMachine.BindCardMachineSearchDTO;
import com.bsj.power.common.netty.ACDeviceStatusData;
import com.bsj.power.common.util.DataUtil;
import com.bsj.power.pls.cache.BindCardMachineCache;
import com.bsj.power.pls.service.BindCardMachineService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description BindCardMachineServiceImpl
 * @time 2024/9/23 17:09
 */
@Service
public class BindCardMachineServiceImpl implements BindCardMachineService {


    /**
     * 分页展示绑卡机信息
     *
     * @param bindCardMachineSearchDTO
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List<com.bsj.power.common.netty.ACDeviceStatusData>>
     * @author ljx
     * @time 2024/9/23 19:22
     */
    @Override
    public JsonResult<List<ACDeviceStatusData>> page(BindCardMachineSearchDTO bindCardMachineSearchDTO) {
        String deviceId = bindCardMachineSearchDTO.getDeviceId();
        if (StringUtils.isNotBlank(deviceId)) {
            ACDeviceStatusData acDeviceStatusData = BindCardMachineCache.getACDeviceStatusData(deviceId);
            return JsonResult.success(List.of(acDeviceStatusData), NumericalConstants.ONE);
        } else {
            List<ACDeviceStatusData> acDeviceStatusDataList = BindCardMachineCache.getAllACDeviceStatusData();
            if (CollectionUtils.isEmpty(acDeviceStatusDataList)) {
                return JsonResult.success(new ArrayList<>(), NumericalConstants.ZERO);
            }
            List<ACDeviceStatusData> result = DataUtil.pagingByList(acDeviceStatusDataList, bindCardMachineSearchDTO.getPageSize(), bindCardMachineSearchDTO.getPageNumber());
            for (ACDeviceStatusData acDeviceStatusData : result) {
                acDeviceStatusData.setStockInfos(BindCardMachineCache.getStockInfoList(acDeviceStatusData.getDeviceId()));
            }
            return JsonResult.success(result, acDeviceStatusDataList.size());
        }
    }
}
